package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.expimp.util.AbstractExpUtil;
import com.ccp.dev.qbdms.expimp.util.AuxiliaryUtil;
import com.ccp.dev.qbdms.model.WQbdUnzeroWarning;
import com.ccp.dev.qbdms.model.WQbdXhdh;
import com.ccp.dev.qbdms.model.vo.UnZeroProblemVo;
import com.ccp.dev.qbdms.service.WQbdFalProblemresetService;
import com.ccp.dev.qbdms.service.WQbdUnzeroWarningService;
import com.ccp.dev.qbdms.service.WQbdXhdhService;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 未归零质量问题统计 信息操作处理控制器
 *
 * @author sun
 * @date 2020-05-06 15:57:36
 */
@Controller
@Action(customModel = "未归零质量问题统计")
@RequestMapping("/qbdms/unZero")
public class UnZeroListController extends SysBaseController {

    @Resource
    private WQbdFalProblemresetService wQbdFalProblemresetService;
    @Resource
    private WQbdXhdhService wQbdXhdhService;
    @Resource
    private WQbdUnzeroWarningService  wQbdUnzeroWarningService;

    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX+ UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "未归零质量问题统计")
    public String viewUnZeroList( ModelMap modelMap){
        // 型号代号
        List<WQbdXhdh> wQbdXhdhs = wQbdXhdhService.getAll();
        modelMap.put("wQbdXhdhs", JSON.toJSONString(wQbdXhdhs));
        return "unZero/list";
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: sun
     * @date: 2020-05-06 15:57:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "附带检索条件的分页查询未归零质量问题", detail = "查询未归零质量问题数据信息<#if success>成功<#else></#if>")
    public Object unZeroList(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        ZhiLiangConst.addQueryFilter(queryFilter);
        queryFilter.put("byId",false);
        List<UnZeroProblemVo> pageDatas = wQbdFalProblemresetService.getUnZeroList(queryFilter);
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }

    /**
     * 功能描述: 跳转到未归零问题预警设置页面
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-12-25 19:21:40
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/earlyWarning")
    @Action(description = "跳转到未归零问题预警设置页面", exectype = "页面跳转日志")
    public String viewEarlyWarning(ModelMap modelMap) {
        WQbdUnzeroWarning aDefault=wQbdUnzeroWarningService.getDefault();
        modelMap.put("aDefault",JSON.toJSONString(aDefault));
        modelMap.put("tenantId",ContextUtil.getCurrentUserTenantId());
        return "unZero/earlyWarning";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: wQbdUnzeroWarning 保存的对象
     * @return: java.lang.Object
     * @auther: WYL
     * @date: 2020-07-20 11:05:14
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存未归零问题预警数据",detail="<#if isAdd>新增<#else>编辑</#if>未归零问题预警数据<#if success>成功<#else>失败</#if>")
    public Object save(@RequestBody WQbdUnzeroWarning wQbdUnzeroWarning) {
        String id = wQbdUnzeroWarning.getId();
        int result;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd",true);
            //新增操作
            //新增数据前生成随机Id
            wQbdUnzeroWarning.setId(ProjectUUIDUtils.getUUIDFor32());
            wQbdUnzeroWarning.setTenantId(ContextUtil.getCurrentUserTenantId());
            result = wQbdUnzeroWarningService.add(wQbdUnzeroWarning);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            //修改操作
            result = wQbdUnzeroWarningService.update(wQbdUnzeroWarning);
        }
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }

    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"id"
     * @return: java.lang.Object
     * @auther: WYL
     * @date: 2020-07-20 11:05:14
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询未归零问题预警的一条数据",detail="根据主键id查询未归零问题预警的数据<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("id") String id) {
        WQbdUnzeroWarning wQbdUnzeroWarning = wQbdUnzeroWarningService.getOneByTenantId(id);
        if (null == wQbdUnzeroWarning) {
            wQbdUnzeroWarning=wQbdUnzeroWarningService.getDefault();
            wQbdUnzeroWarning.setId("");
            wQbdUnzeroWarning.setTenantId(ContextUtil.getCurrentUserTenantId());
        }
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return getResultDataSuccess("get.success",wQbdUnzeroWarning);
    }

    /**
     * 功能描述: 跳转到导出选择字段页面
     *
     * @return: java.lang.String
     * @auther: hw
     * @date: 2019-12-25 19:21:40
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
    @Action(description="跳转到未归零质量问题导出选择字段页面", exectype = "页面跳转日志")
    public String export(HttpServletRequest request,ModelMap modelMap) throws Exception {
        JSONObject jsonObject = AuxiliaryUtil.resolverModel(UnZeroProblemVo.class);
        modelMap.put("tableInfo",jsonObject.toString());
        return "expImp/add";
    }

    /**
     * 功能描述: 导出未归零质量问题数据信息
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: hw
     * @date: 2020-05-25 15:16:44
     */
    @RequestMapping("/exportData")
    @Action(description="导出未归零质量问题数据信息",detail="导出未归零质量问题数据信息<#if success>成功<#else></#if>")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception{
        SysAuditThreadLocalHolder.putParamerter("success",false);
        String[] id = RequestUtil.getStringAryByStr(request,"id");
        QueryFilter queryFilter = new QueryFilter(request,false);
        JSONArray dataArray = ZhiLiangConst.exportAddConditions(queryFilter,id);
        List<UnZeroProblemVo> unZeroList = this.wQbdFalProblemresetService.getUnZeroList(queryFilter);
        if(unZeroList.isEmpty()){
            unZeroList.add(new UnZeroProblemVo());
        }
        new AbstractExpUtil() {
            @Override
            public String customAnalysis(String fieldName, String fieldValue) {
                return null;
            }
        }.executeExport(request,response,unZeroList,dataArray, AbstractExpUtil.ExpStyle.MANY_SHEET);
        SysAuditThreadLocalHolder.putParamerter("success",true);
    }

}
